package com.etc.config;

import com.etc.entity.Payment;
import com.etc.entity.Room;
import com.etc.service.PaymentService;
import com.etc.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {
    @Autowired
    private RoomService roomService;
    @Autowired
    private PaymentService paymentService;
    //3.添加定时任务
    @Scheduled(cron = "0 39 10 18 * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
        List<Payment> paymentList = new ArrayList<>();
        List<Room> list = roomService.findOccupied();
        for (int i = 0; i < list.size(); i++) {
            Payment payment = new Payment();
            payment.setAmount(list.get(i).getArea()*0.75);
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());//设置起时间
//            cal.add(Calendar.MONTH, 1);//增加一年
            //cal.add(Calendar.DATE, 1);//增加一天
            //cal.add(Calendar.DATE, -10);//减10天
            cal.add(Calendar.MONTH, 1);//增加一个月
            payment.setDeadline(cal.getTime());
            payment.setPubdate(new Date());
            payment.setRoom_id(list.get(i).getId());
            payment.setServices("物业费");
            payment.setStatus(2);
            payment.setId(null);
            paymentList.add(payment);
        }
        paymentService.insertPayments(paymentList);
        System.err.println("执行静态定时任务时间1: " + paymentList);
        System.err.println("执行静态定时任务时间2: " + LocalDateTime.now());
    }
}